package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.utils.DateUtils;
import com.itheima.utils.MD5Utils;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Description:
 * @Author: yp
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    /**
     * 根据手机号码查询会员
     * @param telephone
     * @return
     */
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    /**
     * 注册
     *
     * @param member
     */
    public void register(Member member) {
        //建议把密码加密
        if(member.getPassword() != null && !"".equals(member.getPassword())){
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }

        memberDao.add(member);
    }

    /**
     * 统计每个月分的会员的总数(累加)
     *
     * @param monthList
     * @return
     */
    public List<Integer> findMemberCountByMonths(List<String> monthList) {
        List<Integer> memberCountList = new ArrayList<Integer>();

        //遍历
        for (String date : monthList) {
            //2019-06
            date = date+"-31";
            //查询 SELECT count(*) FROM t_member WHERE regTime <= '2019-06-31'
            Integer memberCount = memberDao.findMemberCountBeforeDate(date);

            memberCountList.add(memberCount);

        }
        return memberCountList;
    }

    public List<Map<String, Object>> findByAge() throws Exception {


        List<Map<String,Object>> list2 = new ArrayList<Map<String, Object>>();


        String nowDate = DateUtils.parseDate2String(Calendar.getInstance().getTime(), "yyyy-MM-dd");

        Map<String,Object> map1 = new HashMap<String,Object>();

        Map<String,String> map11 = new HashMap<String, String>();

        map11.put("start","2011-06-30");
        map11.put("end",nowDate);

        map1.put("name","0-18");
        map1.put("value",memberDao.findByAge(map11));

        Map<String,Object> map2 = new HashMap<String,Object>();


        Map<String,String> map22 = new HashMap<String, String>();

        map22.put("start","2000-06-30");
        map22.put("end","2011-06-30");


        map2.put("name","19-30");
        map2.put("value",memberDao.findByAge(map22));
        Map<String,Object> map3 = new HashMap<String,Object>();


        Map<String,String> map33 = new HashMap<String, String>();

        map33.put("start","1985-06-30");
        map33.put("end","2000-06-30");


        map3.put("name","31-45");
        map3.put("value",memberDao.findByAge(map33));

        Map<String,Object> map4 = new HashMap<String,Object>();

        Map<String,String> map44 = new HashMap<String, String>();

        map44.put("start","1971-06-30");
        map44.put("end","1985-06-30");

        map4.put("name","46-60");
        map4.put("value",memberDao.findByAge(map44));


        list2.add(map1);
        list2.add(map2);
        list2.add(map3);
        list2.add(map4);


        return list2;
    }

    public List<Map<String, Object>> findBySex() {

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("name","男");

        map.put("value",memberDao.findBySex("1"));

        Map<String, Object> map2 = new HashMap<String, Object>();

        map2.put("name","女");
        map2.put("value",memberDao.findBySex("2"));

        list.add(map);
        list.add(map2);


        return list;
    }
}
